package cn.zyb.controller;

import cn.zyb.domain.Movie;
import cn.zyb.domain.Ticket;
import cn.zyb.domain.User;
import cn.zyb.feignclient.UserFeignClient;
import cn.zyb.service.IMoveService;
import cn.zyb.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/movie")
public class MovieController {

    @Autowired
    private IMoveService moveService;
    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 电影票查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable("id")  Long id){
        Movie movie = moveService.findOne(id);
        if (movie == null){
            return new Result(false,"请输入正确id",null);
        }
        Result userResult = userFeignClient.findOne(movie.getUser_id());
        movie.setUser(userResult.getResultObj());
        return new Result(userResult.getSuccess(),userResult.getMsg(),movie);
    }

    /**
     * 电影票购买
     * @param user
     * @return
     */
    @PostMapping("/buy")
    public Result BuyOneTicket(@RequestBody User user){
        //查询电影信息
        Movie movie = moveService.findOne(user.getMovie_ticket_id());
        //查询用户信息
        Object userData = userFeignClient.findOne(user.getId()).getResultObj();
        Map entity = (Map)userData;
        String balanceStr = entity.get("balance").toString();
        BigDecimal balance = new BigDecimal(balanceStr);
        //判断用户余额是否足够
        if(balance.compareTo(movie.getPrice())==-1){ //余额不足以买票
            return new Result(false,"余额不足,请充值!!!",null);
        }
        //扣减用户余额
        user.setBalance(balance.subtract(movie.getPrice()));
        Result userRes = userFeignClient.update(user);
        if(!userRes.getSuccess()){
            return new Result(userRes.getSuccess(),userRes.getMsg(),null);
        }
        //在电影服务中新增用户与电影票数据   status在添加数据库时写死为0
        moveService.addMovieTicket(user.getId(),movie.getId());
        return new Result(true,"购买成功",null);
    }

    /**
     * 电影票观看
     * @param user
     * @return
     */
    @PostMapping("/watch")
    public Result Watch(@RequestBody User user){
        //查询出此用户有几张可用的电影票
        List<Ticket> tickets = moveService.findTicketByUserId(user);
        if(tickets==null || tickets.size()==0) {
            return new Result(false,"对不起,您没有未使用的电影票,请先购买电影票!!",null);
        }
        //更改其中一张票的电影票状态
        moveService.updateTicketByUserId(user);
        return new Result(true,"检票成功!!!祝你观影愉快!!!",null);
    }

    @PostMapping("/addOneTicket")
    public Result addOneTicket(@RequestBody User user){
        try {
            moveService.addMovieTicket(user.getId(), user.getMovie_ticket_id());
            return new Result(true,"电影票赠送成功!!!",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"系统繁忙!!!",null);
        }
    }
}

